package com.ysxsoft.wdg.ui.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseFragment;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.UserInfoBean;
import com.ysxsoft.wdg.bean.response.DaiKaiResponse;
import com.ysxsoft.wdg.bean.response.DaiLiStatusResponse;
import com.ysxsoft.wdg.bean.response.MyFenSiResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.LevelRechargeActivity;
import com.ysxsoft.wdg.ui.activity.home.ShareRuleActivity;
import com.ysxsoft.wdg.ui.activity.ht.MyHtActivity;
import com.ysxsoft.wdg.ui.activity.login.LoginValid;
import com.ysxsoft.wdg.ui.activity.my.AccountActivity;
import com.ysxsoft.wdg.ui.activity.my.CollectionActivity;
import com.ysxsoft.wdg.ui.activity.my.KanActivity;
import com.ysxsoft.wdg.ui.activity.my.MyDaiKaiMenuWebViewActivity;
import com.ysxsoft.wdg.ui.activity.my.MyFightActivity;
import com.ysxsoft.wdg.ui.activity.my.MyQuanActivity;
import com.ysxsoft.wdg.ui.activity.my.MyShopActivity;
import com.ysxsoft.wdg.ui.activity.my.partner.MyFsActivity;
import com.ysxsoft.wdg.ui.activity.my.qydl.QuDai1Activity;
import com.ysxsoft.wdg.ui.activity.my.ShopRz1Activity;
import com.ysxsoft.wdg.ui.activity.my.jf.MyJfActivity;
import com.ysxsoft.wdg.ui.activity.my.MyKfActivity;
import com.ysxsoft.wdg.ui.activity.my.MySyActivity;
import com.ysxsoft.wdg.ui.activity.my.PersonActivity;
import com.ysxsoft.wdg.ui.activity.my.SettingActivity;
import com.ysxsoft.wdg.ui.activity.my.partner.MyPartnerActivity;
import com.ysxsoft.wdg.ui.activity.order.OrderActivity;
import com.ysxsoft.wdg.ui.activity.shopcart.ShopcartActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.delayaction.Action;
import com.ysxsoft.wdg.util.delayaction.SingleCall;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.transform.FastSpeedLinearLayoutManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

public class MyFragment extends BaseFragment {


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.iv_tx)
    ImageView ivTx;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_vip)
    TextView tvVip;
    @BindView(R.id.need)
    TextView need;
    @BindView(R.id.tv_id)
    TextView tvID;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private String uid = "";
    private String[] levelArray;
    MyMenuAdapter adapter;
    private String name, icon;

    @Override
    protected int getLayoutId() {
        return R.layout.fm_five;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);

        titleFinish.setImageResource(R.mipmap.my_setting);
        titleIvR.setImageResource(R.mipmap.my_cart);
        titleContent.setText("个人中心");

        levelArray = getResources().getStringArray(R.array.user_level);

    }

    @Override
    public void onResume() {
        super.onResume();
        uid = SharePrefUtils.getUserId();
        mApiHelper.getUserInfo(uid, new Observer<BaseBean<UserInfoBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<UserInfoBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    UserInfoBean infoBean = baseBean.getData();

                    tvUsername.setText(infoBean.getNickname());
                    tvID.setText("ID：" + infoBean.getUid());
                    tvInviteCode.setText("邀请码：" + infoBean.getCode());

//                    int levle = infoBean.getLevle();
//                    if (levle > 0 && levle < 7) {
//                        tvVip.setText(levelArray[levle - 1]);
//                    }
                    tvVip.setText(infoBean.getLevles());

                    ViewUtils.loadCircleImage(mContext, infoBean.getAvatar(), ivTx);
                    name = infoBean.getNickname();
                    icon = infoBean.getAvatar();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
            }
        });

        getMenuList();
    }

    @Override
    protected void setListener() {

    }


    @OnClick({R.id.title_finish, R.id.ll_qydl, R.id.ll_sjrz,
            R.id.ll_kf, R.id.tv_vip,R.id.need})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish://设置
                SettingActivity.start(mContext);
                break;
            case R.id.ll_qydl://区域代理
                QuDai1Activity.start(mContext);
                break;
            case R.id.ll_sjrz://商家入驻
                getStatus();
                break;
            case R.id.ll_kf://我的客服
                MyKfActivity.start(mContext);
                break;
            case R.id.tv_vip://充值升级
//                startActivity(new Intent(getActivity(), ShareRuleActivity.class));
                LevelRechargeActivity.start(getActivity());
                break;
            case R.id.need://充值升级
                LevelRechargeActivity.start(getActivity());
                break;
        }
    }

    @OnClick({R.id.title_iv_r, R.id.ll_user, R.id.ll_order,
            R.id.ll_pt, R.id.ll_kj, R.id.ll_ht, R.id.ll_yhq, R.id.ll_zh,
            R.id.ll_jf, R.id.ll_hhr, R.id.ll_sy, R.id.ll_collection})
    public void onViewClicked2(final View view) {
        SingleCall.getInstance().addAction(new Action() {
            @Override
            public void call() {
                switch (view.getId()) {
                    case R.id.title_iv_r://购物车
                        ShopcartActivity.start(mContext);
                        break;
                    case R.id.ll_user://用户
                        PersonActivity.start(mContext);
                        break;
                    case R.id.ll_order://我的订单
                        OrderActivity.start(mContext);
                        break;
                    case R.id.ll_pt://我的拼团
                        MyFightActivity.start(getActivity());
                        break;
                    case R.id.ll_kj://我的砍价
                        KanActivity.start(getActivity());
                        break;
                    case R.id.ll_ht://我的汇淘
                        Intent intent = new Intent(getActivity(), MyHtActivity.class);
                        intent.putExtra("name", name == null ? "" : name);
                        intent.putExtra("icon", icon == null ? "" : icon);
                        startActivity(intent);
                        break;
                    case R.id.ll_yhq://我的优惠券
                        MyQuanActivity.start(mContext);
                        break;
                    case R.id.ll_zh://我的账户
                        AccountActivity.start(mContext);
                        break;
                    case R.id.ll_jf://我的积分
                        MyJfActivity.start(mContext);
                        break;
                    case R.id.ll_hhr://我的合伙人
                        MyPartnerActivity.start(mContext);
                        break;
                    case R.id.ll_sy://我的收益
                        MySyActivity.start(mContext);
                        break;
                    case R.id.ll_collection://我的收藏
                        CollectionActivity.start(mContext);
                        break;
                }
            }
        }).addValid(new LoginValid(mContext, 1)).doCall();
    }

    /**
     * 获取菜单
     */
    private void getMenuList() {
        OkHttpUtils.post()
                .url(UrlHost.MY_DAI)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DaiKaiResponse daiKaiResponse = JsonUtils.parseByGson(response, DaiKaiResponse.class);
                        if (daiKaiResponse != null) {
                            if (CallbackCode.SUCCESS.equals(daiKaiResponse.getCode())) {//请求成功
                                List<DaiKaiResponse.DataBean> list = daiKaiResponse.getData();
                                if (list != null) {
                                    initAdapter(list);
                                } else {
                                }
                            } else {
//                                ToastUtils.showToast("获取个人中心菜单出错!");
                            }
                        }
                    }
                });
    }

    private void initAdapter(List<DaiKaiResponse.DataBean> datas) {
        if (adapter == null) {
            recyclerView.setNestedScrollingEnabled(false);
            recyclerView.setFocusable(false);
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
            adapter = new MyMenuAdapter(R.layout.item_my_menu);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    DaiKaiResponse.DataBean item = (DaiKaiResponse.DataBean) adapter.getItem(position);
                    MyDaiKaiMenuWebViewActivity.start(getActivity(), item.getTitle(), "" + item.getDid());
                }
            });
        }
        adapter.setNewData(datas);
    }

    class MyMenuAdapter extends BaseQuickAdapter<DaiKaiResponse.DataBean, BaseViewHolder> {
        public MyMenuAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, DaiKaiResponse.DataBean item) {
            Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.image));
            helper.setText(R.id.tv_ct, item.getTitle());
        }
    }


    /**
     * 获取区域代理状态
     */
    private void getStatus() {
        OkHttpUtils.post()
                .url(UrlHost.SHANG_STATUS)
                .addParams("uid", SharePrefUtils.getUserId())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    DaiLiStatusResponse daiLiStatusResponse = JsonUtils.parseByGson(response, DaiLiStatusResponse.class);
                    if (daiLiStatusResponse != null) {
                        if (CallbackCode.SUCCESS.equals(daiLiStatusResponse.getCode())) {
                            int data = daiLiStatusResponse.getData();//  1未申请，2审核中，3已审核
                            if (data == 1) {
                                ShopRz1Activity.start(mContext);
                            } else if (data == 2) {
                                ShopRz1Activity.start(mContext);
                            } else if (data == 3) {
                                Intent intent = new Intent(getActivity(), MyShopActivity.class);
                                startActivity(intent);
                            }
                        }
                    }
                } else {
//                    multStatusView.showError();
                }
            }
        });
    }

}
